#!/usr/bin/env node

/**
 * Debug script to test task parsing
 */

import { readFile } from 'fs/promises';
import { join } from 'path';
import { parseTasksFromMarkdown } from './dist/core/task-parser.js';

async function debugTaskParser() {
  try {
    const specPath = join(process.cwd(), '.spec-workflow', 'specs', 'test-integration', 'tasks.md');
    console.log('📁 Reading spec file:', specPath);

    const content = await readFile(specPath, 'utf-8');
    console.log('📄 File content length:', content.length);
    console.log('📝 First 200 characters:');
    console.log(content.substring(0, 200));
    console.log('...\n');

    // Test line by line
    const lines = content.split('\n');
    console.log('\n🔍 Line by line analysis:');
    lines.forEach((line, i) => {
      const regex = /^\s*-\s+\[([ x\-])\]/;
      const match = regex.test(line);
      if (line.trim().startsWith('- [')) {
        console.log(`Line ${i}: "${line}" -> Match: ${match}`);
      }
    });

    const result = parseTasksFromMarkdown(content);
    console.log('\n🔍 Parse result:');
    console.log('Total tasks found:', result.tasks.length);
    console.log('Summary:', result.summary);

    if (result.tasks.length > 0) {
      console.log('\n📋 Tasks:');
      result.tasks.forEach((task, i) => {
        console.log(`${i + 1}. ID: ${task.id}, Description: ${task.description.substring(0, 50)}...`);
        console.log(`   Status: ${task.status}, Completed: ${task.completed}`);
      });
    }

  } catch (error) {
    console.error('❌ Debug failed:', error.message);
  }
}

debugTaskParser();